import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:iea/provider/base/base_resp.dart';
import 'package:iea/blocs/indexPage_blocs/index_openClass_bloc.dart';
import 'package:iea/models/indexPage_models/index_openClass_model.dart';
import 'package:iea/widgets/loading.dart';
class OpenCourse extends StatefulWidget{
  @override 
  State<StatefulWidget> createState() => _OpenCourseState();
}

class _OpenCourseState extends State<OpenCourse>
  with AutomaticKeepAliveClientMixin<OpenCourse> {
  @override
  bool get wantKeepAlive => true;
  GetopenClassListBloc _bloc = new GetopenClassListBloc();
  ScrollController _controller = new ScrollController();
  List<IndexOpenClassModel> indexOpenList = [];
  _getIndexBanner() {
    Map<String, String>  params = {'type': '1'};
    _bloc.getopenClassList(params);
  }
  @override
  void initState() {
    super.initState();
    _getIndexBanner();
  }
  @override 
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context){
    super.build(context);
    ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return StreamBuilder<BaseResp>(
      stream: _bloc.getopenClassListStream,
      builder:  (context, AsyncSnapshot<BaseResp> snapshot) {
        if (snapshot.data != null && snapshot.hasData) {
          var response = snapshot.data;
          if (response.result == true && response.data != null && response.data.length > 0) {
            indexOpenList = response.data;
            return Column(
              children: <Widget>[
                Container(
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.symmetric(vertical: 13.0, horizontal: 17.0),
                  child: Text('试听课',
                    style: TextStyle(color: Colors.black, fontSize: 17.0, fontWeight: FontWeight.bold),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    print('试听课');
                  },
                  child: indexOpenList[0].bannerUrl == null ? Container() : Padding(
                    padding: EdgeInsets.symmetric(horizontal: 17.0),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(8.0),
                      child: FadeInImage.memoryNetwork(
                        placeholder: kTransparentImage,
                        image: indexOpenList[0].bannerUrl,
                        fit: BoxFit.fill,
                        width: MediaQuery.of(context).size.width,
                        height: ScreenUtil().setHeight(156),
                      ),
                    ),
                  ),
                ),
              ],
            );
          } else {
            return Container();
          }
        } else {
          return LoadingPage();
        }
      },
    );
  }
}